/************************************************************************/
/*Copyright (c) James Zhao 2007,2008
/*This file is part of AdminOverlay.
/*AdminOverlay is free software: you can redistribute it and/or modify
/*it under the terms of the GNU General Public License as published by
/*the Free Software Foundation, either version 3 of the License, or
/*(at your option) any later version.
/*
/*AdminOverlay is distributed in the hope that it will be useful,
/*but WITHOUT ANY WARRANTY; without even the implied warranty of
/*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/*GNU General Public License for more details.
/*
/*You should have received a copy of the GNU General Public License
/*along with Foobar.  If not, see <http://www.gnu.org/licenses/>.                                                                     */
/*************************************************************************/
#include "UIManager.h"

UIManager* UIManager::instance = NULL;

UIManager::UIManager() : LocatableVisual()
{
	root = new UIOverlayRoot();
	root->Parent(NULL);
	UIManager::instance = this;
	isActive = true;
	isMouseDown = false;
	isCtrlDown = false;
	needCursorChange = true;
	wasCursorVisible = true;
	mousePosition.setX(0); mousePosition.setY(0);

	cursorQuad = new QuadVisual(0,0,15,15,0xFF00FF00, 0xFF00FF00);
}
UIManager::~UIManager(void)
{
}

void UIManager::draw( LPDIRECT3DDEVICE9 device )
{
	if(isActive == true)
	{
		root->Width(Width());
		root->Height(Height());
		root->layout(QPoint(0,0));
		root->draw(device);
		cursorQuad->X(mousePosition.x());
		cursorQuad->Y(mousePosition.y());
		cursorQuad->draw(device);
	}
}

void UIManager::onDeviceReset()
{
	root->onDeviceReset();
	cursorQuad->onDeviceReset();
}
UIManager* UIManager::currentInstance()
{
	return instance;
}
void UIManager::onRawKeyDown(int keyCode)
{	
	if(keyCode == VK_CONTROL)
	{
		isCtrlDown = true;
	}
	root->processOnKeyDown(keyCode);
}
void UIManager::onRawKeyUp( int keyCode)
{
	if(keyCode == VK_CONTROL)
	{
		isCtrlDown = false;
	}
	root->processOnKeyUp(keyCode);
	root->processOnKeyPress(keyCode, isCtrlDown);
}
void UIManager::onRawMouseMove(QPoint point)
{
	mousePosition = point;
	root->processOnMouseMove(point, isMouseDown);	
}

void UIManager::onRawMouseDown(QPoint point )
{
	isMouseDown = true;
	root->processOnMouseDown(point);
}

void UIManager::onRawMouseUp( QPoint point )
{
	isMouseDown = false;
	root->processOnMouseUp(point);
}

UIControl* UIManager::getRoot()
{
	return root;
}

bool UIManager::IsActive() const
{
	return isActive;
}

void UIManager::IsActive( bool val )
{
	if(isActive != val)
	{
		isActive = val;
		needCursorChange = true;
	}
}

bool UIManager::IsMouseDown() const
{
	return isMouseDown;
}

void UIManager::IsMouseDown( bool val )
{
	isMouseDown = val;
}